﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using StedySoft.SenseSDK;

namespace Transportr
{
    public partial class CtlBrowse : UserControl
    {
        private SenseTextboxControl tbFilter;
        private BrowserModel model;

        public CtlBrowse()
        {
            model = new BrowserModel();
            InitializeComponent();

            this.tbFilter = new SenseTextboxControl()
            {
                Dock = DockStyle.Top
            };
            this.listLines = new SenseListControl()
            {
                Dock = System.Windows.Forms.DockStyle.Fill,
                FocusedItem = null,
                IsSecondaryScrollType = false,
                MinimumMovement = 15,
                Name = "listLines",
                ShowScrollIndicator = true,
                Springback = 0.35F,
                TabIndex = 1,
                ThreadSleep = 100,
                TopIndex = 0,
                Velocity = 0.9F
            };
            this.Controls.Add(this.listLines);
            this.Controls.Add(this.tbFilter);
            this.tbFilter.BringToFront();
            tbFilter.TextChanged += new EventHandler(tbFilter_TextChanged);
            model.Updated += new UpdatedHandler(model_Updated);
            this.listLines.AddItem(new SensePanelItem()
            {
                PrimaryText = "Downloading data...",
                PrimaryTextAlignment = StedySoft.SenseSDK.DrawingCE.SenseAPIs.SenseFont.PanelTextAlignment.Middle,
                PrimaryTextLineHeight = StedySoft.SenseSDK.DrawingCE.SenseAPIs.SenseFont.PanelTextLineHeight.All
            });
            model.BeginUpdate();
        }

        void tbFilter_TextChanged(object sender, EventArgs e)
        {
            if (model.IsDataLoaded)
            {
                model.Filter.Text = tbFilter.Text;
                model_Updated();
            }
        }

        void model_Updated()
        {
            this.Invoke((MethodInvoker)delegate()
            {
                this.listLines.Clear();
                this.Populate();
            });
        }

        void Populate()
        {
            this.listLines.BeginUpdate();
            foreach (LineItemViewModel l in model.FilteredItems)
            {
                SensePanelItem itm = new SensePanelItem()
                {
                    Name = l.lnId.ToString(),
                    Thumbnail = l.LineIcon,
                    PrimaryText = l.LineNumber,
                    SecondaryText = l.Route
                };
                itm.OnClick += new SensePanelItem.ClickEventHandler(itm_OnClick);
                this.listLines.AddItem(itm);
            }
            this.listLines.EndUpdate();
        }

        void itm_OnClick(object Sender)
        {
            int lnId = Int32.Parse((Sender as SensePanelItem).Name);
            Form frm = new Form();
            CtlLineDetails view = new CtlLineDetails(lnId)
                {
                    Dock = DockStyle.Fill
                };
            frm.Controls.Add(view);
            frm.ShowDialog();
        }
    }
}
